package src.cs2340.todoApp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
 * controls what the buttons on the first page do
 * @author Rochelle
 */
public class WelcomeScreen extends Activity{
	
	private String FILENAME = "databaseFile.txt";
	protected static File dbFile;
	
	@Override
	protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //sets the view of the screen to the welcome.xml's layout
        setContentView(R.layout.welcome);
        
        //instantiates login button that was created in welcome.xml
        Button loginButton = (Button) findViewById(R.id.loginbutton);
        
        //instantiates login button that was created in welcome.xml
        Button registerButton = (Button) findViewById(R.id.registerbutton);
        
        LocationList.init();
        
		try {
			// Check to see if the db file exists
			// Will throw an exception if it does not exist
			openFileInput(FILENAME);
		} catch (FileNotFoundException e) {
			try {
				// Creates an empty db file
				openFileOutput(FILENAME, MODE_PRIVATE);
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		// Create a static reference to the db file
		dbFile = new File(getFilesDir().getAbsolutePath() + "/" + FILENAME);
		
		// This code should be used to hardcode db values
		try {
			FileOutputStream os = openFileOutput(FILENAME, Context.MODE_PRIVATE);
			String s = "username password Rochelle aa@b.com\nusername1 password1 Jordan apple@b.com\n";
			os.write(s.getBytes());
			os.close();
			Account a = new Account("username", "password", "Rochelle", "aa@b.com", Database.getInstance());
			Database.getInstance().addAccount(a);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AddAccountException e2) {
			// TODO First account adding failed
			Log.e("WelcomeScreen", "Adding first account failed! "+e2.toString());
			e2.printStackTrace();
		}
        
        //creates an onClickListener for login button, so that the screen
        // knows what to do next if login button was clicked
        loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//creates an intent to change from the current screen to the LoginScreen
				Intent loginIntent = new Intent(getApplicationContext()/*WelcomeScreen.this*/, LoginScreen.class);
				
				//changes the screen
				WelcomeScreen.this.startActivity(loginIntent);
			}
		});
        
        //creates an onClickListener for register button, so that the screen
        // knows what to do next if register button was clicked
        registerButton.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View v) {
				//creates an intent to change from the current screen to the RegisterScreen
				Intent registerIntent = new Intent(getApplicationContext(), RegisterScreen.class);
				
				//changes screen
				startActivity(registerIntent);
			}
		});
	}
}
